﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.IO;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using FmcWebsite.Core;
using FmcWebsite.Web;

namespace Cms
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class gallerydata : IHttpHandler
    {
        const string root_dir = @"D:\Projects\FmcWebsite\Cms\";

        public void ProcessRequest(HttpContext context)
        {
            WebArticle article = null;

            if (context.Request.Params["code"] != null)
            {
                article = WebArticle.ByCode(context.Request.Params["code"], context.Session);
            }

            if (context.Request.Params["id"] != null)
            {
                article = WebArticle.ById(int.Parse(context.Request.Params["id"]), context.Session);
            }

            if (article != null)
            {
                context.Response.ContentType = "text/xml";                
                var settings = (new StreamReader(root_dir + "gallerydata.xml")).ReadToEnd();

                var images = "";

                foreach (AttachmentIMAGE img in article.Images)
                {                    
                    images +=
                        string.Format("\r\n<image imageURL=\"/AttachmentImage.ashx?id={0}\" thumbURL=\"/thumbnail.ashx?id={1}\" linkURL=\"\" linkTarget=\"\" >",
                                      img.Id,img.Id);
                    images += "\r\n<caption /></image>\r\n";
                }

                context.Response.Write(string.Format(settings, article.Images.Count() > 1 ? "RIGHT" : "RIGHT", images));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
